/**
 * 
 */
package co.fitstart.payment.web.handler.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.BeanUtils;
import com.demo2do.core.utils.SignatureUtils;
import com.demo2do.core.wechat.pay.WechatPayNotification;

import co.fitstart.payment.entity.app.App;
import co.fitstart.payment.web.handler.SignatureHandler;

/**
 * @author lute
 *
 */
@Component("signatureHandler")
public class SignatureHandlerImpl implements SignatureHandler {

    private static final Logger logger = LoggerFactory.getLogger(SignatureHandlerImpl.class);
    
    /* (non-Javadoc)
     * @see cn.xcm.payment.web.handler.RequestHandler#checkAppSignature(cn.xcm.payment.entity.app.App, java.lang.String, java.util.Map, java.lang.String[])
     */
    public boolean checkAppSignature(App app, String signature, Map<String, String> parameters, String... ignoreKeys) {
        
        if (app != null) {
            
            // Parse the parameter to sorted query string
            String sortedQueryString = com.demo2do.core.utils.StringUtils.toSortedQueryString(parameters, ignoreKeys) + "&appsecret=" + app.getAppsecret();
            logger.trace("SignatureHandlerImpl#checkAppSignature() - Get sorted query string [{}].", sortedQueryString);
            
            // Make MD5 encryption and check equals
            String encryptedText = SignatureUtils.makeMD5Signature(sortedQueryString).toUpperCase();
            logger.trace("SignatureHandlerImpl#checkAppSignature() - Get encypted text [{}].", encryptedText);
            
            return StringUtils.equals(encryptedText, signature);
            
        }
        
        return false;
    }

    /* (non-Javadoc)
     * @see co.fitstart.payment.web.handler.SignatureHandler#checkWechatNotificationSignature(com.demo2do.core.wechat.pay.WechatPayNotification, java.lang.String)
     */
    public boolean checkWechatNotificationSignature(WechatPayNotification notification, String paySecret) {
        
        // Parse notification and get the to be encrypted query string
        Map<String, String> parameters = BeanUtils.pureDescribeWithKeyUnderscored(notification);
        parameters.putAll(notification.fromCouponLists());
        parameters.remove("coupon_fees");
        parameters.remove("coupon_ids");
        
        String queryString = com.demo2do.core.utils.StringUtils.toSortedQueryString(parameters, "sign") + "&key=" + paySecret;
        
        logger.trace("SignatureHandlerImpl#checkWechatNotificationSignature() - Get the to be encrypted query string [{}].", queryString);
        
        String encryptedString = SignatureUtils.makeMD5Signature(queryString).toUpperCase();
        String sign = notification.getSign();
        
        logger.trace("SignatureHandlerImpl#checkWechatNotificationSignature() - Get encrypted string [{}] and signature [{}].", encryptedString, sign);
        
        return StringUtils.equals(encryptedString, sign);
        
    }

}
